package com.redisson.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.IOException;

/**
 * @作者: 林江
 * @创建时间: 2024/2/10
 * @功能:
 */
@Configuration
@EnableRedisRepositories(basePackages = "com.redisson.mapper")
public class RedisConfig {

    @Bean
    public RedisTemplate redis(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(RedisSerializer.string()); // key序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //值序列化
        return template;

    }

    // redisson分布式开源框架
    @Bean(destroyMethod = "shutdown")
    RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://127.0.0.1:6379");
//                .setAddress("redis://192.168.32.128:10000").setPassword("123456");
        return Redisson.create(config);
    }
}
